package com.yunyao.common.utils;


import com.yunyao.common.core.redis.RedisCache;
import com.yunyao.common.dto.dict.DataDictionary;
import com.yunyao.common.web.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.List;

/**
 * 字典缓存工具类
 * @author chenyejian
 * @date 2024-07-21
 */
@Lazy
public class DictCacheUtils {
    private static Logger logger = LoggerFactory.getLogger(DictCacheUtils.class);
    @Autowired
    private static RedisCache redisCache = SpringContextUtils.getBean(RedisCache.class);

    private static final String DICT_KEY_PREFIX = "dict:";

    public DictCacheUtils() {
    }

    /**
     * 获取字典列表
     * @param groupCode
     * @return
     */
    public static List<DataDictionary> getDictList(String groupCode) {
        String reidsKey =DICT_KEY_PREFIX + groupCode.replace("_","").toUpperCase();
        List<DataDictionary> dictionaryList = null;
        try {
            if (redisCache.hasKey(reidsKey)) {
                dictionaryList = redisCache.getCacheList(reidsKey, DataDictionary.class);
                return dictionaryList;
            }
        } catch (Exception e) {
            logger.warn("读取字典缓存失败!",e);
        }
        return dictionaryList;
    }

    /**
     * 清除字典列表缓存
     * @param reidsKey
     * @return
     */
    public static Boolean clearDictionaryListFromCache(String reidsKey) {
//        String reidsKey =DICT_KEY_PREFIX + groupCode.replace("_","").toUpperCase();
        return redisCache.deleteObject(reidsKey);
    }

    /**
     * 根据字典组和字典key获取字典值
     * @param groupCode
     * @param dictKey
     * @return
     */
    public static String getDictValue(String groupCode, String dictKey) {
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        if (dictionaryList == null || dictionaryList.size() == 0) {
            /**
             * 字典不存在，返回当前值dictKey
             */
            return dictKey;
        }
        return dictionaryList.stream().filter(d -> d.getKey().equals(dictKey)).map(DataDictionary::getValue).findFirst().orElse("");
    }

    /**
     * 根据字典组和字典value获取字典值
     * @param groupCode
     * @param dictValue
     * @return
     */
    public static String getDictKey(String groupCode, String dictValue) {
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        if (dictionaryList == null || dictionaryList.size() == 0) {
            /**
             * 字典不存在，返回当前值dictValue
             */
            return dictValue;
        }
        return dictionaryList.stream().filter(d -> d.getValue().equals(dictValue)).map(DataDictionary::getKey).findFirst().orElse("");
    }


    /**
     * 根据字典组和字典key获取字典对象
     * @param groupCode
     * @param dictKey
     * @return
     */
    public static DataDictionary getDictData(String groupCode, String dictKey) {
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        if (dictionaryList == null || dictionaryList.size() == 0) {
            /**
             * 字典不存在，返回当前值dictKey
             */
            return null;
        }
        return dictionaryList.stream().filter(d -> d.getKey().equals(dictKey)).findFirst().orElse(null);
    }


}
